var midiMin = 48;
var midiMax = 72;
var maxFreq = 2000;
var redA = maxFreq*0.1698;
var redB = maxFreq*0.4972;
var redC = maxFreq*0.554364;
var redD = maxFreq*0.932;

var greenA = maxFreq*0.338;
var greenB = maxFreq*0.3898;
var greenC = maxFreq*0.5692;
var greenD = maxFreq*0.62641;
var greenE = maxFreq*0.69846

var blueA = maxFreq*0.65;
var blueB = maxFreq*0.7954;

function valueToMidi(value, minValue, maxValue){
	var midiMin = 48;
	var midiMax = 72;
	//Possibly round this value.
	//alert("midiMin: " + midiMin + ", midiMax: " + midiMax + ", value: " + value + ", minValue: " + minValue + ", maxValue: " + maxValue); 
	//return (value-minValue)/(maxValue-minValue)*(midiMax-midiMin))+midiMin;
	return (value-minValue)/(maxValue-minValue)*(midiMax-midiMin)+midiMin;
	//return 2;
}

function midiToFrequency(midi){
	//alert(midi);
	return 13.75 * Math.pow(2, (midi-9)/12);
}

function frequencyToRed(frequency) {
	var red;
	//RED
	if(frequency<=redA){
	//INCREASE
		red = (frequency/redA);
	}
	//(84.9,248.6)
	//RED-YELLOW
	else if(frequency>redA && frequency<redB){
		red = 1;
	}
	//[248.6,277.182]
	//YELLOW
	else if(frequency>=redB && frequency<=redC){
		//DECREASE
		//390
		red = (redC-frequency)/(redC-redB);
	}
	//PURPLE
	else if(frequency>=redD ){
	//INCREASE UNTIL 500 and then stay constant at 255
		if(frequency<maxFreq){
			red = (frequency-redD)/(redD-maxFreq);
		}
		else{
			red = 1;
		}
	}
	//(277.182,466)
	//GREEN-BLUE
	else{
		red = 0;
	}
	
	return red;
}

function frequencyToGreen(frequency){
	var green;
	//ORANGE
	if(frequency>= greenA && frequency<=greenB){
		green = 0.647 * ((frequency-greenA)/(greenB-greenA));
	}
	//ORANGE-YELLOW-GREEN
	else if(frequency>greenB && frequency<=greenC){
		green = ((frequency-greenB)/(greenC-greenB));
	}
	//GREEN
	else if(frequency>greenC && frequency<=greenD){
		green = 1;
	}
	//GREEN
	else if(frequency>greenD && frequency<=greenE){
		green = (greenE-frequency)/(greenE-greenD);
	}

	else{
		green = 0;
	}
	return green;
}

function frequencyToBlue(frequency){
	var blue;
	//BLUE
	if(frequency>=blueA && frequency <= blueB){
		blue = (blueB-frequency)/(blueB-blueA);
	}
	//BLUE PURPLE
	else if(frequency > blueB){
		blue = 1;
	}
	else{
		blue = 0;
	}
	return blue;
}



